<?php
class StudentResumesController extends AppController {

    public $name = 'StudentResumes';

    public function index(){
        $this->StudentResume->recursive = 0;
        $this->set('studentResumes', $this->paginate());
    }

    public function view($id = null){
        if(!$id){
            $this->Session->setFlash(__('Invalid StudentResume.', true));
            $this->redirect(array('action'=>'index'));
        }
        $this->set('studentResume', $this->StudentResume->read(null, $id));
    }

    /* Add resume record, we need no other parametes but the Student id */
    /* Only called once in StudentResume lifecycle */
    public function add($student_id=null){
        if($student_id){
            $this->loadModel('Student');
            $student = $this->Student->read(null, $student_id);
            if($student['Student']['resume'] == '1'){
                $this->Session->setFlash(__('Resume for you already exists', true));
                $this->redirect(array('action'=>'edit', $this->StudentResume->field('id', 'student_id='.$student_id)));
            }
            $this->data['StudentResume']['student_id'] = $student_id;
            $this->StudentResume->create();
            if($this->StudentResume->save($this->data)){
                $this->Student->id = $student_id;
                $this->Student->saveField('resume', 1);
                $this->Session->setFlash(__('The StudentResume has been saved', true));
                $this->redirect(array('action'=>'edit', $this->StudentResume->getLastInsertId()));
            }else{
                $this->Session->setFlash(__('The StudentResume could not be saved. Please, try again.', true));
            }
        }
        $this->redirect(array('controller'=>'students','action'=>'uhome'));
    }

    public function edit($id = null){
        if(!$id && empty($this->data)){
            $this->set('status',__('Invalid StudentResume', true));
            $this->redirect(array('controller'=>'students','action'=>'uhome'));
        }
        if(!empty($this->data)){
            if($this->StudentResume->save($this->data)){
                $this->set('status',__('The StudentResume has been saved', true));
                $this->redirect(array('controller'=>'students','action'=>'uhome'));
            }else{
                $this->set('status',__('The StudentResume could not be saved. Please, try again.', true));
            }
        }
        if(empty($this->data)){
            $this->data = $this->StudentResume->read(null, $id);
        }
    }

    public function delete($id = null){
        if(!$id){
            $this->Response->json(false, __('Invalid id for StudentResume', true));
        }
        if($this->StudentResume->del($id)){
            $this->Response->json(true, __('StudentResume deleted', true));
        }
    }

}
?>